package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.SerialPortUtils;

public class TestPopupWindow extends PopupWindow implements View.OnClickListener , PopupWindow.OnDismissListener {

    private Context mContext;

    private View view;

    TextView tvInfo;

    public TestPopupWindow(Context context){
        mContext = context;
        view = LayoutInflater.from(context).inflate(R.layout.popupwindow_test, null);

        Button btn = view.findViewById(R.id.btn);
        tvInfo = view.findViewById(R.id.tv_info);

        btn.setOnClickListener(this);

        this.setContentView(view);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(this);

    }

    public void show(Activity context) {
        showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    int norml = 1;
    int slop = 1;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn:
                slop++;
                byte[] nowelAndSlopeInstruct = BluetoothOrder.getInstance().getNowelAndSlopeInstruct(norml, slop);
                SerialPortUtils.getInstance().sendMsg(nowelAndSlopeInstruct);
                break;
        }
    }

    @Override
    public void onDismiss() {

    }
}
